/**
 * Created by ZYY on 2016/12/1.
 */
/*获取经纬度json数据*/
// $.getJSON(url, function (data) {
/*var id = new Array();
 var message = new Array();
 var action = new Array();*/
/*var flag = data.length;*/
//  $.each(data.jsonArray, function(index) {
/*id[flag] = data.jsonArray[index].id;
 message[flag] = data.jsonArray[index].message;
 action[flag] = data.jsonArray[index].action;*/
//  flag ++;
//    });
var data=[
    {id:1001,coordinate:"116.331398,39.897445",message:"附加消息"},
    {id:1001,coordinate:"116.331398,39.897445",message:"附加消息"},
    {id:1001,coordinate:"116.331398,39.897445",message:"附加消息"},
    {id:1001,coordinate:"113.29776032, 25.51924935",message:"附加消息"},
    {id:1001,coordinate:"113.29776032, 25.51924935",message:"附加消息"},
    {id:1001,coordinate:"106.82690749, 28.77273891",message:"附加消息"},
    {id:1001,coordinate:"106.82690749, 28.77273891",message:"附加消息"},
    {id:1001,coordinate:"110.16677412, 20.84082833",message:"附加消息"},
    {id:1001,coordinate:"120.43121832, 27.17108269",message:"附加消息"},
    {id:1001,coordinate:"118.70117995, 24.85031054",message:"附加消息"},
    {id:1001,coordinate:"118.70117995, 24.85031054",message:"附加消息"},
    {id:1001,coordinate:"101.67569514, 22.47902628",message:"附加消息"},
    {id:1001,coordinate:"101.67569514, 22.47902628",message:"附加消息"},
    {id:1001,coordinate:"113.02427868, 29.77063003",message:"附加消息"},
    {id:1001,coordinate:"113.02427868, 29.77063003",message:"附加消息"},
    {id:1001,coordinate:"113.73410300, 29.59432100",message:"附加消息"},
    {id:1001,coordinate:"110.09233013, 29.79501288",message:"附加消息"},
    {id:1001,coordinate:"121.70238386, 29.97723935",message:"附加消息"},
    {id:1001,coordinate:"121.70238386, 29.97723935",message:"附加消息"},
    {id:1001,coordinate:"118.89830319, 27.46554153",message:"附加消息"},
    {id:1001,coordinate:"112.67760402, 29.59291143",message:"附加消息"},
    {id:1001,coordinate:"119.24112624, 35.12484476",message:"附加消息"},
    {id:1001,coordinate:"119.24112624, 35.12484476",message:"附加消息"},
    {id:1001,coordinate:"119.24112624, 35.12484476",message:"附加消息"},
    {id:1001,coordinate:"118.89830319, 27.46554153",message:"附加消息"},
    {id:1001,coordinate:"114.98907379, 31.47163968",message:"附加消息"},
    {id:1001,coordinate:"119.76419035, 31.17910208",message:"附加消息"},
    {id:1001,coordinate:"119.76419035, 31.17910208",message:"附加消息"},
    {id:1001,coordinate:"119.76419035, 31.17910208",message:"附加消息"},
    {id:1001,coordinate:"114.87756114, 32.97349918",message:"附加消息"},
    {id:1001,coordinate:"114.87756114, 32.97349918",message:"附加消息"},
    {id:1001,coordinate:"89.54669617, 35.85727018",message:"附加消息"},
    {id:1001,coordinate:"90.27748815, 38.23823486",message:"附加消息"},
    {id:1001,coordinate:"90.27748815, 38.23823486",message:"附加消息"},
    {id:1001,coordinate:"118.52190658, 34.71420656",message:"附加消息"},
    {id:1001,coordinate:"96.39753411, 38.28168800",message:"附加消息"},
    {id:1001,coordinate:"96.39753411, 38.28168800",message:"附加消息"}
];
var provinceData = [];  //创建省 json
var cityData = [];   //创建市 json
$.each(data, function(index) {
    //第一步：先将经纬度坐标转化成城市：省+市（地级城市）
    var asList = data[index].coordinate.toString().split(",");  //分割经纬度
    //调用百度地图API方法
    var map = new BMap.Map("allmap");
    var point = new BMap.Point(asList[0], asList[1]);
    var gc = new BMap.Geocoder();
    gc.getLocation(point, function (rs) {
        //获得返回json数据格式：省：province 市：city 县/区：district 镇/街道：street 村/街道号streetNumber
        var addComp = rs.addressComponents;

        var province = {};
        //地图需要：只提供省名称
        if(addComp.province.substring(0,2) == "黑龙" || addComp.province.substring(0,2) == "内蒙"){
            province = {name:addComp.province.substring(0,3),selected:false,value:1};
        }else {
            province = {name:addComp.province.substring(0,2),selected:false,value:1};
        }
        var city = {};
        //百度地图API中BUG，直辖市与省同级，但比省少一个地级城市，他们下一级是区，为了统一，所有直辖市都有地级城市，为自己
        //此处排除四个直辖市的地级城市，直接获得区级城市
        if(addComp.city == "北京市" || addComp.city == "天津市" || addComp.city == "上海市" || addComp.city == "重庆市"){
            city = {name:addComp.district,value:1}
        }else {
            city = {name:addComp.city,value:1}
        }
        //将省、市分别加入json
        cityData.push(city);
        provinceData.push(province);//将省份加入json数组中
    });
});
//配置路径
setTimeout(function () {
    require.config({
        paths: {
            echarts: 'js/build/dist',
            zrender: 'js/src'
        }
    });
    require(
        [
            'echarts',
            'echarts/chart/map'  //需要加入map js文件
        ],
        function (ec) {
            // 基于准备好的dom，初始化echarts图表
            var myChart = ec.init(document.getElementById('main'));

            var option = {
                title: {
                    text: '机器人销售区域统计',
                    subtext: '中国',
                    x: 'left'
                },
                tooltip: {
                    trigger: 'item',
                },
                dataRange: {
                    min: 0,
                    max: 10,
                    //color: ['orange', 'yellow'],
                    text: ['高', '低'], // 文本，默认为数值文本
                    calculable: true
                },
                series: [{
                    name: '',
                    type: 'map',
                    mapType: 'china',
                    selectedMode: 'single',
                    itemStyle: {
                        normal: {
                            label: {
                                show: true
                            }
                        },
                        emphasis: {
                            label: {
                                show: true
                            }
                        }
                    },
                    data: provinceData,
                }],

            };
            /*var ecConfig = require('echarts/config');*/
            var ecConfig = require('echarts/config');
            var zrEvent = require('zrender/tool/event');
            var curIndx = 0;
            var mapType = [
                'china',
                // 23个省
                '广东', '青海', '四川', '海南', '陕西',
                '甘肃', '云南', '湖南', '湖北', '黑龙江',
                '贵州', '山东', '江西', '河南', '河北',
                '山西', '安徽', '福建', '浙江', '江苏',
                '吉林', '辽宁', '台湾',
                // 5个自治区
                '新疆', '广西', '宁夏', '内蒙古', '西藏',
                // 4个直辖市
                '北京', '天津', '上海', '重庆',
                // 2个特别行政区
                '香港', '澳门'
            ];

            //地图点击事件
            myChart.on(ecConfig.EVENT.MAP_SELECTED, function (param) {
                var len = mapType.length;
                var mt = mapType[curIndx % len];
                if (mt == 'china') {
                    // 全国选择时指定到选中的省份
                    var selected = param.selected;
                    for (var i in selected) {
                        if (selected[i]) {
                            mt = i;
                            while (len--) {
                                if (mapType[len] == mt) {
                                    curIndx = len;
                                }
                            }
                            break;
                        }
                    }
                    option2.series[0].mapType = mt;
                    option2.title.subtext = mt + ' （点击返回中国地图）';
                    myChart.setOption(option2, true);
                } else {
                    curIndx = 0;
                    mt = 'china';
                    myChart.setOption(option, true);
                }

            });
            var option2 = {
                title: {
                    text: '机器人销售统计',
                    subtext: '中国',
                    x: 'left'
                },
                tooltip: {
                    trigger: 'item',

                },

                dataRange: {
                    min: 0,
                    max: 10,
                    //color: ['orange', 'yellow'],
                    text: ['高', '低'], // 文本，默认为数值文本
                    calculable: true
                },
                series: [{
                    name: '',
                    type: 'map',
                    mapType: 'china',
                    selectedMode: 'single',
                    itemStyle: {
                        normal: {
                            label: {
                                show: true
                            }
                        },
                        emphasis: {
                            label: {
                                show: true
                            }
                        }
                    },
                    data: cityData,
                }]
            };
            myChart.setOption(option, true);
        });
},1000)
